﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WebCamDeskLib
{
    /// <summary>
    /// Klasse für die Verwaltung nummerierter Dateinamen. Durch Abruf des Dateinamens wird der Zähler
    /// automatisch erhöht.
    /// GetNextFilename() -> "File1.jpg"
    /// GetNextFileName() -> "File2.jpg"
    /// </summary>
    public class PictureManager
    {
        public string PathName { get; set; }
        public string PictureName { get; set; }
        private int PictureCounter { get; set; }

        private string fCurrentFileName;
        
        public PictureManager()
        {
            PictureCounter = 0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PictureManager"/> class.
        /// </summary>
        /// <param name="aPathName">Name of a path.</param>
        /// <param name="aFileName">Name of a file.</param>
        /// <param name="aCounter">A counter.</param>
        public PictureManager(string aPathName, string aFileName, int aCounter)
        {
            PictureCounter = aCounter;
            PathName = aPathName;
            PictureName = aFileName;
        }

        /// <summary>
        /// Gets the name of the next file. FileNumber is automaticaly increased
        /// </summary>
        /// <returns></returns>
        public string GetNextFileName()
        {
            PictureCounter++;
            try
            {
                // führende Nullen für die Sortierung
                string myFile = Path.GetFileNameWithoutExtension(PictureName);
                string numbers = PictureCounter.ToString();
                string myPaddedNr = numbers.PadLeft(5, '0');
                myFile = myFile + myPaddedNr + Path.GetExtension(PictureName);
                
                fCurrentFileName = Path.Combine(PathName, myFile);
                return fCurrentFileName; 
            }
            catch (Exception e)
            {
                MessageBox.Show("Fehler bei der Pfaderstellung: "+ e.Message);
                return null;
            }
        }
        /// <summary>
        /// Gets the name of the current file.
        /// </summary>
        /// <returns></returns>
        public string GetCurrentFileName()
        {
            return fCurrentFileName;
        }
        
    }
}
